package com.zxy.demo.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zxy.demo.common.ModelResult;
import com.zxy.demo.common.TaskItemStatusEnum;
import com.zxy.demo.common.utils.ClazzUtils;
import com.zxy.demo.common.utils.JsonUtils;
import com.zxy.demo.domain.pojo.TaskItem;
import com.zxy.demo.domain.vo.AddTaskVo;
import com.zxy.demo.domain.vo.UpdateTaskVo;
import com.zxy.demo.service.TaskItemService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags={"任务管理Controller"})
@Controller
@RequestMapping("/task")
public class TaskItemController {
	private static final String ADD_TASK_PAGE = "addTask";
	private static final String HOME_PAGE = "home";
	private static final String UPDATE_TASK_PAGE = "updateTask";
	private static List<String> taskClazzNameList;
	private static Map<String,String> taskStatusMap;
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private TaskItemService service;
	
	/** 静态代码块来初始化 task类名集合以及状态Map集合 */
	static {
		taskClazzNameList = ClazzUtils.getClazzName("com.zxy.demo.task.entity", false);
		if(!CollectionUtils.isEmpty(taskClazzNameList)){
			taskClazzNameList.remove("com.zxy.demo.task.entity.BaseTask");
		}
		TaskItemStatusEnum[] values = TaskItemStatusEnum.values();
		taskStatusMap = new LinkedHashMap<>(values.length);
		for (TaskItemStatusEnum taskItemStatusEnum : values) {
			taskStatusMap.put(taskItemStatusEnum.getStatus().toString(), taskItemStatusEnum.getTips());
		}
	}
	
	/** 跳转到管理页面 */
	@ApiIgnore	// 这个方法不在swagger文档上显示
	@GetMapping("/home")
	public String home(Model model) {
		List<TaskItem> list = service.findAll();
		model.addAttribute("listData", list);
		model.addAttribute("taskStatusMap", taskStatusMap);
		return HOME_PAGE;
	}
	
	/** 跳转到添加页面 */
	@ApiIgnore	// 这个方法不在swagger文档上显示
	@GetMapping("/toAddTask")
	public String toAddTask(Model model) {
		model.addAttribute("clazzList", taskClazzNameList);
		return ADD_TASK_PAGE;
	}
	
	/** 添加功能 */
	@ApiOperation (value = "添加任务接口", httpMethod = "POST", response = ModelResult.class)
	@PostMapping("/addTask")
	@ResponseBody
	public ModelResult<Void> addTask(@Valid @RequestBody AddTaskVo vo , BindingResult bindingResult) {
		ModelResult<Void> result = new ModelResult<Void>();
		// 使用JSR303进行简单的参数校验
		if (bindingResult.hasErrors()) {
			result.setMsgWithDefaultErrorCode(bindingResult.getFieldErrors().get(0).getDefaultMessage());
			log.info("[TaskItemController addTask method vo validate error] vo is -->{} and error info is -->{}",
					JsonUtils.object2Json(vo), JsonUtils.object2Json(bindingResult.getFieldErrors()));
			return result;
		}
		result = service.add(vo);
		return result;
	}
	
	/** 更新状态 */
	@ApiOperation(value="启用或者停用接口", httpMethod = "POST" ,response = ModelResult.class)
	@PostMapping("/updateStatus")
	@ResponseBody
	public ModelResult<Void> updateStatus(int id, int status) {
		ModelResult<Void> result = new ModelResult<Void>();
		service.updateStatus(id,status);
		return result;
	}
	
	/** 删除 */
	@ApiOperation(value="删除接口", httpMethod = "POST" ,response = ModelResult.class)
	@PostMapping("/delete")
	@ResponseBody
	public ModelResult<Void> delete(int id) {
		ModelResult<Void> result = new ModelResult<Void>();
		service.delete(id);
		return result;
	}
	
	/** 跳转到编辑页面 */
	@ApiIgnore
	@GetMapping("/toUpdate")
	public String toUpdate(Model model, int id) {
		TaskItem item = service.findById(id);
		model.addAttribute("task", item);
		return UPDATE_TASK_PAGE;
	}
	
	/** 编辑 */
	@ApiOperation(value="更新接口", httpMethod = "POST", response = ModelResult.class)
	@PostMapping("/update")
	@ResponseBody
	public ModelResult<Void> update(@RequestBody @Valid UpdateTaskVo vo, BindingResult bindingResult) {
		ModelResult<Void> result = new ModelResult<>();
		// 使用JSR303进行简单的参数校验
		if (bindingResult.hasErrors()) {
			result.setMsgWithDefaultErrorCode(bindingResult.getFieldErrors().get(0).getDefaultMessage());
			log.info("[TaskItemController update method vo validate error] vo is -->{} and error info is -->{}",
					JsonUtils.object2Json(vo), JsonUtils.object2Json(bindingResult.getFieldErrors()));
			return result;
		}
		service.update(vo);
		return result;
	}
	
	
	
}
